package com.df.web;

import com.df.web.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created by Administrator on 2016/9/14.
 */
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableFeignClients
public class TestWebApp {

    @Autowired
    private LoadBalancerClient loadBalancer;
    @Autowired
    private DiscoveryClient discovery;
    @Autowired
    RestTemplate rest;

    @Autowired
    private TestService appClient;


    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @RequestMapping("/test")
    String test() {
        System.out.println(loadBalancer.choose("tomcat-test"));
        loadBalancer.choose("tomcat-test");
//        String str = rest.getForObject("http://tomcat-test/hi", String.class);
        System.out.println(discovery.getServices());

        return appClient.getMsg() + ",,," + appClient.getOpt() + ",,," + appClient.getTra();
    }

    @RequestMapping("/testEntity")
    String testEntity() {
//        TestEntity testEntity = new TestEntity();
//        testEntity.setUserName("action");
//        testEntity.setThisTime(new Date());
//        testEntity.setId(666L);
//        System.out.println(appClient.getUserModel(12333).toString());
        System.out.println(appClient.testSave());
        return "success";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(TestWebApp.class, args);
    }
}
